<template>
  <div class="my_warp app_content">
    <!-- 导航栏 -->
    <div class="header_body">
      <header-nav :leftArrow="false" titelText="我的"></header-nav>
    </div>
    <div class="user" v-if="!state.userInfo.id">
      <div class="userImg" @click="logout">
        <img width="49" height="49" :src="swiperImage" alt="" />
        <span>请登录</span>
      </div>
    </div>
    <div class="user" v-if="state.userInfo.id">
      <div class="userImg">
        <img
          width="49"
          height="49"
          :src="state.userInfo.avator ? state.userInfo.avator : swiperImage"
          alt=""
        />
        <div>
          <div>{{ state.userInfo.name }}</div>
          <div class="sign">
            {{ state.userInfo.sign || "这个人很懒，什么都没有留下" }}
          </div>
        </div>
      </div>

      <!-- 体重管理 -->
      <div class="weights">
        <div class="each">
          <div>{{ state.weightInfo.cur }}</div>
          <div>当前（公斤）</div>
        </div>
        <div class="each">
          <div>{{ state.weightInfo.tar }}</div>
          <div>目标（公斤）</div>
        </div>
        <div class="each">
          <div>{{ state.weightInfo.bmi }}</div>
          <div>bmi</div>
        </div>
      </div>
    </div>
    <div>
      <div class="menus">
        <div class="flex-center each-menu gap8" @click="info">
          <van-icon class="fs22" name="friends-o" />
          <div>个人信息</div>
        </div>
        <div class="flex-center each-menu gap8" @click="toTeacher">
          <van-icon class="fs22" name="service-o" />
          <div>在线咨询</div>
        </div>
        <div class="flex-center each-menu gap8" @click="logout">
          <van-icon class="fs22" name="revoke" />
          <div>退出登录</div>
        </div>
        
        <div class="chart" v-if="state.userInfo.id">
          <div id="main" style="width: 100vw; height: 160px"></div>
        </div>
      </div>
    </div>

    <!-- 底部 -->
    <div class="footer_body">
      <footer-nav></footer-nav>
    </div>
  </div>
</template>

<script setup name="My">
import moment from 'moment'
import swiperImage from "@/assets/images/defaultUser.jpg";

import * as echarts from "echarts";
import { getRecord } from "@/api/http.js";
const router = useRouter();

const state = reactive({
  // 用户下面的收藏信息等
  weightInfo: {
    cur: 0,
    tar: 0,
    bmi: "-",
  },
  userInfo: {},
});

const logout = () => {
  router.push("/login");
};

const toTeacher = () => {
  router.push("/teacher");
}

const info = () => {
  router.push("/info");
};


const renderPie = () => {
  setTimeout(() => {
    let myChart = echarts.init(document.getElementById("main"));
    let option = {
      series: [
        {
          name: "Pressure",
          type: "gauge",
          progress: {
            show: true,
            overlap: false,
            roundCap: true,
            clip: false,
            itemStyle: {
              borderWidth: 1,
              borderColor: "#464646",
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: false,
            distance: 50,
          },
          pointer: {
            show: false,
          },
          splitLine: {
            show: false,
            distance: 0,
            length: 10,
          },
          detail: {
            valueAnimation: true,
            formatter: "{value}",
            fontSize: 20,
            show: false,
          },
          data: [
            {
              value: state.weightInfo.bmi * 100 / 30,
              name: `BMI：${state.weightInfo.bmi}`,
              detail: {
                offsetCenter: ["0%", "-30%"],
              },
            },
          ],
        },
      ],
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
  }, 100);
};

onMounted(() => {
  console.log(localStorage.getItem("appUserInfo"));
  if (localStorage.getItem("appUserInfo")) {
    state.userInfo = JSON.parse(localStorage.getItem("appUserInfo"));
    state.weightInfo.tar = state.userInfo.targetweight;
    getRecord(state.userInfo.id).then((res) => {
      console.log(res)
      let rs = res.data.sort((a, b) => {
        return moment(b.time).valueOf() - moment(a.time).valueOf();
      });
      if (rs.length) {
        state.weightInfo.cur = rs[0].weight;
        // 计算bmi
        console.log(state.weightInfo.cur, state.userInfo.height);
        if (state.weightInfo.cur && state.userInfo.height) {
          let height = Number(state.userInfo.height);
          state.weightInfo.bmi = (state.weightInfo.cur / (height * height)).toFixed(2);
          renderPie()
        }
      }
    });
  }
});
</script>
<style lang="less" scoped>
.flex-center {
  display: flex;
  align-items: center;
}
.app_content {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.weights {
  width: 90%;
  height: 60px;
  padding: 20px;
  margin-top: 12px;
  display: flex;
  justify-content: space-between;
  font-size: 16px;

  .each {
    display: flex;
    flex: 1;
    flex-direction: column;

    div {
      width: 100%;
      text-align: center;
    }
  }
}
.menus {
  width: 80%;
  padding: 20px;
  display: flex;
  flex-direction: column;
  font-size: 14px;

  .each-menu {
    width: 100%;
    height: 50px;
    background: #fff;
    border-radius: 10px;
    padding: 0 20px;
    margin-bottom: 12px;
  }
}
.fs22 {
  font-size: 22px;
  margin-right: 4px;
}
.user {
  width: 100%;
  height: 200px;
  background: -webkit-linear-gradient(top, #ff8198, #f585cb);
  margin-bottom: 10px;
}
.userImg {
  display: flex;
  align-items: center;
  margin-left: 20px;
  padding-top: 30px;
  padding-bottom: 10px;
  img {
    border-radius: 100%;
    margin-right: 10px;
  }
  span {
    color: white;
    font-weight: 600;
  }
  .sign {
    font-size: 14px;
    color: #333;
    margin-top: 4px;
  }
}
.user-collection {
  ul {
    display: flex;
    align-items: center;
    li {
      display: flex;
      align-items: center;
      flex-direction: column;
      font-size: 14px;
      flex: 1;
      color: white;
    }
  }
}
</style>
